#region License, Terms and Conditions
//
// Prawks - Copyright (c) 2007 Atif Aziz. All rights reserved.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//
#endregion

// 
// This is a modified version of the code originally written by:
//
//  Peter Hallam
//      C# Compiler Developer
//      Microsoft Corp.
//
//  Source: http://www.gotdotnet.com/
//

namespace Utilities
{
    using System;
    using System.IO;
    
    /// <summary>
    /// A delegate used in error reporting.
    /// </summary>
    public delegate void ErrorReporter(string message);

    /// <summary>
    /// Useful Stuff.
    /// </summary>
    public sealed class Utility
    {
        /// <summary>
        /// Don't ever call this.
        /// </summary>
        private Utility() {}
        
        /// <summary>
        /// Parses Command Line Arguments. 
        /// Errors are output on Console.Error.
        /// Use CommandLineArgumentAttributes to control parsing behaviour.
        /// </summary>
        /// <param name="arguments"> The actual arguments. </param>
        /// <param name="destination"> The resulting parsed arguments. </param>
        /// <returns> true if no errors were detected. </returns>
        public static bool ParseCommandLineArguments(string [] arguments, object destination)
        {
            return ParseCommandLineArguments(arguments, destination, new ErrorReporter(Console.Error.WriteLine));
        }
        
        /// <summary>
        /// Parses Command Line Arguments. 
        /// Use CommandLineArgumentAttributes to control parsing behaviour.
        /// </summary>
        /// <param name="arguments"> The actual arguments. </param>
        /// <param name="destination"> The resulting parsed arguments. </param>
        /// <param name="reporter"> The destination for parse errors. </param>
        /// <returns> true if no errors were detected. </returns>
        public static bool ParseCommandLineArguments(string[] arguments, object destination, ErrorReporter reporter)
        {
            CommandLineArgumentParser parser = new CommandLineArgumentParser(destination.GetType(), reporter);
            return parser.Parse(arguments, destination);
        }
        /// <summary>
        /// Returns a Usage string for command line argument parsing.
        /// Use CommandLineArgumentAttributes to control parsing behaviour.
        /// </summary>
        /// <param name="argumentType"> The type of the arguments to display usage for. </param>
        /// <returns> Printable string containing a user friendly description of command line arguments. </returns>
        public static string GetCommandLineArgumentsUsage(Type argumentType)
        {
            return (new CommandLineArgumentParser(argumentType, null)).Usage;
        }
        /// <summary>
        /// Writes a user friendly description of command line arguments to the console.
        /// </summary>
        /// <param name="argumentType"> The type of the arguments to display usage for. </param>
        /// <returns> Printable string containing a user friendly description of command line arguments. </returns>
        public static void WriteCommandLineArgumentsUsage(Type argumentType)
        {
            (new CommandLineArgumentParser(argumentType, null)).WriteUsage(Console.Out);
        }
        /// <summary>
        /// Writes a user friendly description of command line arguments.
        /// </summary>
        /// <param name="argumentType"> The type of the arguments to display usage for. </param>
        /// <param name="writer">Writer to use to write the usage.</param>
        /// <returns> Printable string containing a user friendly description of command line arguments. </returns>
        public static void WriteCommandLineArgumentsUsage(Type argumentType, TextWriter writer)
        {
            (new CommandLineArgumentParser(argumentType, null)).WriteUsage(writer);
        }
    }
}